package cn.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.pojo.User;

public class UserServlet extends HttpServlet {

	

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String result = request.getParameter("action");
		if ("login".equals(result)) {
			this.login(request, response);
		}else if ("doAdd".equals(result)) {
			this.doAdd(request, response);
		}else if ("update".equals(result)) {
			this.update(request, response);
		}else if ("doUpdate".equals(result)) {
			this.doUpdate(request, response);
		}else if ("doDelete".equals(result)) {
			this.doDelete(request, response);
		}
	}
	
	private List<User> list= new ArrayList<User>();
	/**
	 * Constructor of the object.
	 */
	public UserServlet(){
		list.add(new User("admin1","111111",20));
		list.add(new User("admin2","222222",30));
		list.add(new User("admin3","333333",40));
	}
	
	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		String name = request.getParameter("name");
		String passWord = request.getParameter("passWord");
		
		if("admin".equals(name) && "123".equals(passWord)){
			request.getSession().setAttribute("name", name);	
			request.getSession().setAttribute("list", list);	
			request.getRequestDispatcher("/welcome.jsp").forward(request, response);
			return;
		}else {
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			return;
		}
	}
	
	public void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		String name = request.getParameter("name");
		String passWord = request.getParameter("passWord");
		int age = Integer.parseInt(request.getParameter("age"));
		list.add(new User(name,passWord,age));		
		response.sendRedirect("../welcome.jsp");
		return;
	}
	
	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String name = request.getParameter("name");
		String passWord = request.getParameter("passWord");
		int age = Integer.parseInt(request.getParameter("age"));
		User user=new User(name,passWord,age);
		request.getSession().setAttribute("user", user);
		response.sendRedirect("../update.jsp");
		return;
	}
	
	public void doUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String name = request.getParameter("name");
		String passWord = request.getParameter("passWord");
		int age = Integer.parseInt(request.getParameter("age"));
		String userName = request.getParameter("userName");
		for(User user:list){
			if(user.getUserName().equals(userName)){
				list.remove(user);	
				list.add(new User(name,passWord,age));
				break;
			}	
		}
		response.sendRedirect("../welcome.jsp");
		return;
	}
	
	public void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String name = request.getParameter("name");
		for(User user:list){
			if(user.getUserName().equals(name)){
				list.remove(user);
				break;
			}
		}
		response.sendRedirect("../welcome.jsp");
		return;
	}
	
	

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
